#include <string.h>
//#include <graphics.h>
#include "menu.h"
#include "user.h"
#include "gr.h"
#include "message.h"
//#include "bit8.h"
//#include "3dview.h"
//#include "picfile.h"

static char name[40];
static char *names[]={
   "                      ",
   "                      ",
   "                      ",
   "                      ",
   "                      ",
   "                      "
};
static char *defs[]={
   " a.pic                ",
   " a.pic                ",
   " a.pic                ",
   " a.pic                ",
   " a.pic                ",
   " a.pic                "
};


static char tPic32[]="  Output to Pic File (float)";
static char tPic8[]="  Output to Pic File (byte)";
static char tPic1[]="  Output to Pic File (bit)";
static char tBmp8[]="  Output to Bmp File (byte)";
static char tBmp1[]="  Output to Bmp File (bit)";
static char tDat1[]="  Output to Dat File (bit)";
static char fePic[]=".pic";
static char feBmp[]=".bmp";
static char feDat[]=".dat";
static char dt32[]="float";
static char dt8[]="byte";
static char dt1[]="bit";
static char fnPic32[20]="a.pic";
static char fnPic8[20]="a.pic";
static char fnPic1[20]="a.pic";
static char fnBmp8[20]="a.bmp";
static char fnBmp1[20]="a.bmp";
static char fnDat1[20]="a.dat";
static int m=1, oStr=1, oCol=1, scale=1;

void odViewOnly(int ifPart, int ifMask) {
   if( !ifMask && !ifPart ) strcpy(name, "View Whole Image");
   if( !ifMask &&  ifPart ) strcpy(name, "View Part of Image with Scale");
   if(  ifMask && !ifPart ) strcpy(name, "View Whole Mask");
   if(  ifMask &&  ifPart ) strcpy(name, "View Part of Mask with Scale");

   int pn=0;
   if( ifPart ) {
      strcpy(names[0], "Beginning String");
      strcpy(names[1], "Beginning Colomn");
      strcpy(names[2], "Scale");
      sprintf(defs[0], "%d", oStr);
      sprintf(defs[1], "%d", oCol);
      sprintf(defs[2], "%d", scale);
      pn=3;
   }
   if( ifMask ) {
      strcpy(names[pn], "Mask Index");
      sprintf(defs[pn], "%d", m);
      pn++;
   }

   if( pn==0 || sedit(25,5,pn,names,TmpPar,defs,name) ) {
      UndoMenu(2) ;
      if( ifPart ) {
         sscanf(TmpPar[0], "%d", &oStr);
         sscanf(TmpPar[1], "%d", &oCol);
         sscanf(TmpPar[2], "%d", &scale);
         if( oStr<1 ) oStr=1;
         if( oCol<1 ) oCol=1;
         if( scale==0 ) scale=1;
         my_gr_init(scale, oStr-1, oCol-1);
      }
      else my_gr_init(0, 0, 0);
      if( ifMask ) {
         sscanf(TmpPar[pn-1], "%d", &m);
         if( m==0 || m==-1 ) m=1;
         BinType=m;
      }
      else BinType=0;
      OutFile=ftViewOnly;

      if( !ifMask && !ifPart ) sprintf(TmpStr, "Out: View Whole Image");
      if( !ifMask &&  ifPart ) sprintf(TmpStr, "Out: View Image Part");
      if(  ifMask && !ifPart ) sprintf(TmpStr, "Out: View Whole Mask");
      if(  ifMask &&  ifPart ) sprintf(TmpStr, "Out: View Mask Part");
      if( ifMask ) sprintf(name, "Mask Index %d", m);
      else         sprintf(name, " ");
      MessageOutput(TmpStr, name);
   }
}

void odFile(FileType ft) {
   char *fn=0, *fe=0, *t=0, *dt=0;
   int pn=1;
   switch (ft) {
      case ftPic32: fn=fnPic32; fe=fePic; t=tPic32; dt=dt32;       break;
      case ftPic8:  fn=fnPic8;  fe=fePic; t=tPic8;  dt=dt8;        break;
      case ftPic1:  fn=fnPic1;  fe=fePic; t=tPic1;  dt=dt1; pn=2;  break;
      case ftBmp8:  fn=fnBmp8;  fe=feBmp; t=tBmp8;  dt=dt8;        break;
      case ftBmp1:  fn=fnBmp1;  fe=feBmp; t=tBmp1;  dt=dt1; pn=2;  break;
      case ftDat1:  fn=fnDat1;  fe=feDat; t=tDat1;  dt=dt1; pn=2;  break;
      default: fn=0;
   }

   if( fn && fe && t && dt) {
      strcpy(name, t);
      strcpy(names[0], "File Name");
      strcpy(names[1], "Mask Index");
      strcpy(defs[0], fn);
      sprintf(defs[1], "%d", m);
      if( sedit(25,5,pn,names,TmpPar,defs,name) ) {
         UndoMenu(2) ;
         strcpy(fn, TmpPar[0]);
         AdjustPicName(fn, fe);
         if( pn==2 ) {
            sscanf(TmpPar[1], "%d", &m);
            if( m<1 ) m=1;
            if( m>9 ) m=9;
            BinType=m;
         }
         else BinType=0;
         strcpy(OutFileName, fn);
         OutFile=ft;
         sprintf(TmpStr, "Out: to <%s> (%s)", fn, dt);
         if( pn==2 ) sprintf(name, "     Mask Index %d", m);
         else        sprintf(name, " ");
         MessageOutput(TmpStr, name);
         my_gr_init(0, 0, 0);
      }
   }
   else strbox("odFile : Wrong <FileType> ???");
}
